package com.quan.hr.web.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.annotation.OperationLog;
import com.quan.commons.core.MyBaseController;
import com.quan.commons.exception.BizException;
import com.quan.commons.utils.DateUtils;
import com.quan.commons.utils.PageUtils;
import com.quan.commons.utils.WebUtils;
import com.quan.commons.vo.R;
import com.quan.hr.commons.export.HrEmployeeExportVo;
import com.quan.hr.commons.vo.HrEmployeeVo;
import com.quan.hr.entity.HrEmployee;
import com.quan.hr.entity.HrPostCoefficient;
import com.quan.hr.service.HrEmployeeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 员工信息
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2023-02-18 17:12:34
 */
@Slf4j
@Api(tags = "员工信息")
@RestController
@RequestMapping("/hr/hremployee")
public class HrEmployeeController extends MyBaseController {

    @Autowired
    private HrEmployeeService service;

    /**
     * 保存
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "新增")
    @PostMapping("save")
    public R save(@Valid @RequestBody HrEmployeeVo vo) {
        if (this.service.hasExist("work_no", vo.getWorkNo(), HrEmployee.class)) {
            throw new BizException("员工编号已存在");
        }
        this.service.save(vo);
        return R.ok();
    }

    /**
     * 修改
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "修改")
    @PostMapping("update")
    public R update(@Valid @RequestBody HrEmployeeVo vo) {
        this.service.updateById(vo);
        return R.ok();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @OperationLog(module = "员工信息", title = "删除")
    @PostMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        if (null != ids && ids.length > 0) {
            this.service.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 多条件查询信息详情
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "查询详情")
    @GetMapping("/info")
    public R info(HrEmployeeVo vo) {
        QueryWrapper<HrEmployee> queryWrapper = new QueryWrapper<HrEmployee>(vo);
        HrEmployee hrEmployee =this.service.getOne(queryWrapper);
        return R.ok().data(hrEmployee);
    }

    /**
     * 根据ID查询信息详情
     * @param {id}
     * @return
     */
    @OperationLog(module = "员工信息", title = "查询详情")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        HrEmployeeVo vo =this.service.getInfo(id);
        return R.ok().data(vo);
    }

    /**
     * 列表查询
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "查询列表")
    @GetMapping(value = "list")
    public Object list(HrEmployeeVo vo) {
        List<HrEmployeeVo> list = this.service.queryList(new PageUtils(request), vo);
        return R.ok().data(list);
    }

    /**
     * 分页查询
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "查询列表")
    @GetMapping(value = "page")
    public R queryPage(HrEmployeeVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().data(page);
    }

    /**
     * 设置员工状态失败
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "设置员工状态")
    @PostMapping("batchSettingStatus")
    public R batchSettingStatus(@RequestBody HrEmployeeVo vo) {

        if (null != vo.getIds() && !vo.getIds().isEmpty()) {
            this.service.batchSettingStatus(vo);
            return R.ok();
        }
        return R.failure("设置员工状态失败");
    }

    /**
     * 批量调换部门
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "批量调换部门")
    @PostMapping("batchSettingDept")
    public R batchSettingDept(@RequestBody HrEmployeeVo vo) {

        if (null != vo.getIds() && !vo.getIds().isEmpty()) {
            this.service.batchSettingDept(vo);
            return R.ok();
        }
        return R.failure("批量调换部门失败");
    }

    /**
     * 批量调换岗位
     * @param vo
     * @return
     */
    @OperationLog(module = "员工信息", title = "批量调换岗位")
    @PostMapping("batchSettingPost")
    public R batchSettingPost(@RequestBody HrEmployeeVo vo) {

        if (null != vo.getIds() && !vo.getIds().isEmpty()) {
            this.service.batchSettingPost(vo);
            return R.ok();
        }
        return R.failure("批量调换岗位失败");
    }

    /**
     * 导出Excel
     * @param vo
     * @throws IOException
     */
    @PostMapping("export")
    public void code(@RequestBody HrEmployeeVo vo) throws IOException {
        QueryWrapper<HrEmployee> queryWrapper = new QueryWrapper<HrEmployee>(vo);

        List<HrEmployee> list = this.service.list(queryWrapper);

        try {
            if (null != list && !list.isEmpty()) {
                // 组装数据
                List<HrEmployeeExportVo> beanList = new ArrayList<HrEmployeeExportVo>();

                for (HrEmployee user : list) {
                    HrEmployeeExportVo ee = new HrEmployeeExportVo();
                    BeanUtil.copyProperties(user, ee);
                    beanList.add(ee);
                }

                // 设置模板
                TemplateExportParams params = new TemplateExportParams("export/employee.xlsx", true);

                // 模板数据
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("now", DateUtils.getDate());
                map.put("list", beanList);

                // 导出
                Workbook workbook = ExcelExportUtil.exportExcel(params, map);
                // 文件名称
                String filename = "员工信息" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx";
                // 客户端下载
                WebUtils.download(response, workbook, filename);
            }

        } catch (Exception e) {
        }
    }

}
